function formatMonth(month: number | undefined) {
  if (month == null) return null;
  return month < 10 ? `0${month}` : month;
}

export const initvalue = {
  fontSize: 9,
  lineHeight: 10,
  marginHorizontal: 0,
  marginVertical: 0,
  marginVerticaltop: 0,
  marginVerticalbottom: 0,
  spacingFactor: 1,
};
export const PersonalConst: Partial<Record<keyof PersonalDetails, string>> = {
  name: "姓　　名",
  expJob: "求职岗位",
  phoneNumber: "电　　话",
  email: "邮　　箱",
  gender: "性　　别",
  birthDay: "出生日期",
  weightHeight: "身高/体重",
  workingYears: "工作年限",
  arrivalTime: "入职时间",
  expectedSalary: "期望薪资",
  politicsStatus: "政治面貌",
  maritalStatus: "婚姻状况",
  nationality: "民　　族",
  expCity: "意向城市",
  nativePlace: "籍　　贯",
};

export const photoSize = {
  XL: 170,
  L: 150,
  M: 130,
  S: 110,
  XS: 90,
};
export function getPersonalLabel(
  info: PersonalDetails,
  key: keyof PersonalDetails | "weightHeight",
  hasColon?
) {
  let colon = "：";
  if (hasColon === false) {
    colon = "";
  }
  if (key === "birthDay" && info.showAge) {
    return info[key] ? `年　　龄${colon}` : "";
  }
  if (key === "weightHeight") {
    return `身高体重${colon}`;
  }
  return info[key] != null && info[key] !== ""
    ? `${PersonalConst[key]}${colon}`
    : "";
}

export const workingYears = [
  { value: "应届生", label: "应届生" },
  { value: "一年经验", label: "一年经验" },
  { value: "两年经验", label: "两年经验" },
  { value: "三年经验", label: "三年经验" },
  { value: "四年经验", label: "四年经验" },
  { value: "五年经验", label: "五年经验" },
  { value: "六年经验", label: "六年经验" },
  { value: "七年经验", label: "七年经验" },
  { value: "八年经验", label: "八年经验" },
  { value: "九年经验", label: "九年经验" },
  { value: "十年经验", label: "十年经验" },
  { value: "十年以上经验", label: "十年以上经验" },
];
export const politicsStatus = [
  { value: "中共党员", label: "中共党员" },
  { value: "中共预备党员", label: "中共预备党员" },
  { value: "共青团员", label: "共青团员" },
  { value: "群众", label: "群众" },
];
export const gender = [
  { value: 1, label: "男" },
  { value: 0, label: "女" },
];
export const arrivalTime = [
  { value: "随时到岗", label: "随时到岗" },
  { value: "1周内到岗", label: "1周内到岗" },
  { value: "1个月内到岗", label: "1个月内到岗" },
  { value: "2个月内到岗", label: "2个月内到岗" },
  { value: "到岗时间另议", label: "到岗时间另议" },
];

export const educationBackground = [
  { value: 0, label: "小学" },
  { value: 1, label: "初中" },
  { value: 2, label: "高中" },
  { value: 3, label: "中专" },
  { value: 4, label: "大专" },
  { value: 5, label: "本科" },
  { value: 6, label: "硕士" },
  { value: 7, label: "博士" },
  { value: 8, label: "博士后" },
  { value: 9, label: "MBA" },
];

export const skillsOptions = [
  {
    value: 0,
    label: "一般",
  },
  {
    value: 1,
    label: "良好",
  },
  {
    value: 2,
    label: "熟练",
  },
  {
    value: 3,
    label: "精通",
  },
];
const skillsOptionMap = skillsOptions.reduce((acc, cur) => {
  acc[cur.value] = cur.label;
  return acc;
}, {});
export function getSkillName(v?: number) {
  return skillsOptionMap[v];
}

export function getPersonalItem(
  info: PersonalDetails,
  key: keyof PersonalDetails
): React.ReactNode {
  if (key === "birthDay") {
    if (!info.showAge) {
      if (!info[key] || !info[key]?.year) return "";

      return `${info[key]?.year || ""}${
        info[key]?.year && info[key]?.month ? "-" : ""
      }${formatMonth(info[key]?.month) || ""}`;
    } else if (info[key]?.year) {
      return `${
        new Date().getFullYear() - new Date(`${info[key]?.year}`).getFullYear()
      }岁`;
    }
    return "";
  }
  if (key === "gender") {
    return gender.find((g) => g.value === info?.[key])?.label;
  }
  if (key === "politicsStatus") {
    return politicsStatus.find((g) => g.value === info?.[key])?.label;
  }
  if (key === "workingYears") {
    return workingYears.find((g) => g.value === info?.[key])?.label;
  }
  if (key === "weightHeight") {
    const weightHeight = info?.[key];
    if (weightHeight) {
      const { weight, height } = weightHeight;
      if (weight && height) {
        return `${height}cm/ ${weight}kg`;
      }
      if (weight) {
        return ` ${weight}kg`;
      }
      if (height) {
        return ` ${height}cm`;
      }
    }
    return "";
  }
  // if (key === 'weight') {
  //   return info?.[key] ? `${info?.[key]}kg` : ''
  // }
  if (key === "arrivalTime") {
    return arrivalTime.find((g) => g.value === info?.[key])?.label;
  }
  return info?.[
    key as keyof Omit<
      PersonalDetails,
      "showAge" | "birthDay" | "detailsOrder" | "photoObj" | "weightHeight"
    >
  ];
}

const educationBackgroundMap = educationBackground.reduce((acc, cur) => {
  acc[cur.value] = cur.label;
  return acc;
}, {});

export function getEducationBackground(v?: number) {
  return educationBackgroundMap[v];
}

export function formatDate(date: IDate | null) {
  if (!date) return "";
  if (date?.untilToday) {
    return "至今";
  }
  return `${date?.year || ""}${date.year && date.month ? "." : ""}${
    date.year && date?.month ? formatMonth(date?.month) : ""
  }`;
}

export function getTimeRange(startDate: IDate | null, endDate: IDate | null) {
  const start = formatDate(startDate);
  const end = formatDate(endDate);
  return `${start}${start && end ? "-" : ""}${end}`;
}

export function getRgb(color: any) {
  if (!color) return "";
  return `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a})`;
}

export function extractStyles(html) {
  // 定义正则表达式，匹配所有 <style> 标签及其内容，包括属性
  const styleRegex = /<style\b[^>]*>([\s\S]*?)<\/style>/gi;
  const styles = [];
  let match;

  // 使用正则表达式查找所有匹配项
  // eslint-disable-next-line no-cond-assign
  while ((match = styleRegex.exec(html)) !== null) {
    styles.push(match[0]?.replace(/\/\*[\s\S]*?\*\//g, ""));
  }
  // 创建去除 <style> 标签后的 HTML 字符串
  const htmlWithoutStyles = html.replace(styleRegex, "");

  return [styles, htmlWithoutStyles];
}
